var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
$CertusFormAjax.ajax('findWaitInfo',{"processId":processId},function (response) {
    var data = response.data.appInfo;
    $$scope.id = data.id;
    $$scope.tripNum = data.trip_num;
    $$scope.userName = data.user_name;
    $$scope.planStime = data.trip_star_time;
    $$scope.planEtime = data.trip_end_time;
    // $$scope.tripComments = data.trip_comments;
    var bugetInfo = response.data.bugetInfo;
    $$scope.spMoney = 0;
    for (var i=0;i<bugetInfo.length;i++){
        if (bugetInfo[i].status==1){
            $$scope.spMoney = bugetInfo[i].buget;
        }
    }
    $$scope.actualAplStarTime = data.actual_trip_time;
    $$scope.actualAplEndTime = data.actual_end_time;
    if(data.type==1){
        $$scope.type ="出差";
    }else{
        $$scope.type ="公干";
    }
});

//查询审批人职位
$CertusFormAjax.ajax('getSpPosition',{},function (response) {
    var data = response.data;
    $$scope.position = data;
});

$CertusFormAjax.ajax('findForceBuget',{"processId":processId},function (response) {
    var data = response.data;
    $$scope.bugets = data;
});
loadTriplb();
var yzData = [];
function loadTriplb() {
    $CertusFormAjax.ajax('findTripPlans',{"processId":processId},function (response) {
        var data = response.data;
        yzData = data;
        // 出差计划
        var oabtColumns = [{
            id: 'pro_name',
            title: '归属项目',
            type: 'stri1ng',
            columnClass: 'text-center',
        }, {
            id: 'plan_star_time',
            title: '计划开始时间',
            type: 'date',
            format:'yyyy-MM-dd hh:mm:ss',
            otype:'time_stamp_ms',
            columnClass: 'text-center',
        }, {
            id: 'plan_end_time',
            title: '计划结束时间',
            type: 'date',
            format:'yyyy-MM-dd hh:mm:ss',
            otype:'time_stamp_ms',
            columnClass: 'text-center',
        }, {
            id: 'plan_city',
            title: '地点',
            type: 'string',
            columnClass: 'text-center',
        }, {
            id: 'plan_comments',
            title: '工作计划',
            type: 'string',
            columnClass: 'text-center',
        },{
            id: 'actual_star_time',
            title: '实际开始时间',
            type: 'date',
            format:'yyyy-MM-dd',
            otype:'time_stamp_ms',
            columnClass: 'text-center',
            resolution:function (value, record){
                var content="";
                if (record.actual_star_time!=null){
                    var date = new Date(record.actual_star_time)
                    content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd hh:mm:ss");
                }
                return content;
            }
        }, {
            id: 'actual_end_time',
            title: '实际结束时间',
            type: 'date',
            format:'yyyy-MM-dd',
            otype:'time_stamp_ms',
            columnClass: 'text-center',
            resolution:function (value, record){
                var content="";
                if (record.actual_end_time!=null){
                    var date = new Date(record.actual_end_time)
                    content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd hh:mm:ss");
                }
                return content;
            }
        },{
            id: 'trip_result',
            title: '出差结果',
            type: 'string',
            columnClass: 'text-center',
        },{
            id: 'operation',
            title: '操作',
            width: 100,
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record, column, grid, dataNo, columnNo) {
                var content = '';
                content += '<button class="btn btn-xs btn-info" onclick="toEdit('+record.id + ');"><i class="fa fa-edit"></i>  编辑</button>';
                return content;
            }
        }];
        $("#oabt2Container").empty();
        var oabtOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '出差计划',
            datas: data,
            columns: oabtColumns,
            gridContainer: 'oabt2Container',
            tools: '',
        };
        var oabtGrid = $.fn.dlshouwen.grid.init(oabtOption);
        oabtGrid.load();
    });
}

//显示出差计划表单
function shoabtmeg() {
    $('.hdoabtmeg').show();
}
// 隐藏出差计划表单
function hdoabtmeg() {
    $('.hdoabtmeg').hide();
}
//编辑 查询单个出差项目信息
function toEdit(id) {
    $CertusFormAjax.ajax('findSignPlanInfo',{"id":id},function (response) {
        var data = response.data;
        $$scope.sproId = data.pro_id;
        $$scope.sproName = data.pro_name;
        $$scope.splanStarTime = data.plan_star_time;
        $$scope.splanEndTime = data.plan_end_time;
        $$scope.splanCity = data.plan_city;
        $$scope.splanComments = data.plan_comments;
        $$scope.aid = id;
        $$scope.actualStarTime = data.actual_star_time;
        $$scope.actualEndTime = data.actual_end_time;
        $$scope.tripResult = data.trip_result;
    });
    $(".hdoabtmeg").show();
}

//提交出差结果
$(".tj").on('click',function () {
    var starTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var planResult = $("#planResult").val();
    if (starTime==""){
        layer.msg("实际计划开始时间不能为空");
        return;
    }
    if (endTime==""){
        layer.msg("实际计划结束时间不能为空");
        return;
    }

    var sdate = new Date(starTime);
    var edate = new Date(endTime);
    if (Date.parse(sdate) > Date.parse(edate)) {
        layer.msg("开始时间不能大于结束时间！");
        return;
    }


    if (planResult.trim()==""){
        layer.msg("工作结果不能为空");
        return;
    }
    var id = $("#appId").val();
    $CertusFormAjax.ajax('writeResult',{"id":id,"starTime":starTime,"endTime":endTime,"planResult":planResult},function () {
        layer.msg("填写成功!");
        $('.hdoabtmeg').hide();
        $("#startTime").val("");
        $("#endTime").val("");
        $("#planResult").val("");
        loadTriplb();
    });
});

//取消申请
$(".down").on('click',function () {
    $CertusFormAjax.ajax('shutdownWait',{"id":$$scope.id,"stepId":stepId,"processId":processId},function (response) {
        layer.msg("取消成功！");
        window.history.back();
    });
});
//提交申请
$(".submitTrip").on('click',function () {
    var actualAplStarTime = $("#actualAplStarTime").val();
    var actualAplEndTime = $("#actualAplEndTime").val();
    if (actualAplStarTime==""){
        layer.msg("实际出差开始时间不能为空");
        return;
    }
    if (actualAplEndTime==""){
        layer.msg("实际出差结束时间不能为空");
        return;
    }
    var sdate = new Date(actualAplStarTime);
    var edate = new Date(actualAplEndTime);
    if (Date.parse(sdate) > Date.parse(edate)) {
        layer.msg("实际出差开始时间不能大于实际出差结束时间！");
        return;
    }


    for (var i=0;i<yzData.length;i++){
        if (yzData[i].actual_star_time==undefined||yzData[i].actual_star_time==""){
            layer.msg("出差结果尚未全部填写！");
            return;
        }
        if (yzData[i].actual_end_time==undefined||yzData[i].actual_end_time==""){
            layer.msg("出差结果尚未全部填写！");
            return;
        }
        if (yzData[i].trip_result==undefined||yzData[i].trip_result==""){
            layer.msg("出差结果尚未全部填写！");
            return;
        }
    }
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('writeActResult',{"id":$$scope.id,"stepId":stepId,"processId":processId,"starTime":actualAplStarTime,
    "endTime":actualAplEndTime},function () {
        //关闭
        layer.close(index);
        layer.msg("申请成功！");
        window.history.back();
    });
});